﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 版权所有 (c) 2022   保留所有权利。
 * CLR版本：4.0.30319.42000
 * 机器名称：DESKTOP-UL84BU5
 * 公司名称：
 * 命名空间：KingerRobot.WanliMDC.SpiService.Services.AbnormalBussiness
 * 唯一标识：9c6971df-1af0-406e-b791-33d93c61e7dc
 * 文件名：AbnormalBus
 * 当前用户域：DESKTOP-UL84BU5
 * 
 * 创建者：zzl
 * 电子邮箱：1003590782@qq.com
 * 创建时间：2022/5/24 16:17:38
 * 版本：V1.0.0
 * 描述：异常任务创建
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>

namespace KingerRobot.MexicoSailunMDC.SpiService.Services.AbnormalBussiness;

internal class BAbnormal : IAbnormal
{

    private readonly static IProdProduct _ProdProduct = new BProdProduct();
    private readonly static ITask _Task = new BTask();
    private readonly static IWms2MesService _Wms2MesService = new Wms2MesImpl();
    private readonly static IWcsScanner _WcsScanner = new BWcsScanner();

    public (long taskId, ProdProductBindingBean bean) CreateAbnormal(string productCode, long ptaskId, long locId, string remark)
    {
        if (ptaskId > 0)
        {
            _Task.BakAndDeleteTaskAndCmd(ptaskId, remark);
        }
        // 获取条码信息，没有添加
        var bean = _ProdProduct.CreateAbnormalProductEmptyBinding(productCode);
        if (bean is null)
        {
            return (0, null);
        }
        var skuId = bean.POProdSku.Id;
        var taskId = LocationAbnormalTaskAndCmdRetTaskId(locId, skuId.GetValueOrDefault(), remark, productCode);
        //if (taskId > 0) 
        //{
        //    _WcsScanner.UpdateWcsScannerStatusByProductCode(productCode,(int)EnumScannerStatus.NoCreate,"生成异常任务")
        //}

        return (taskId, bean);
    }
    //走环形口
    public (long taskId, ProdProductBindingBean bean) CreateCircularAbnormal(string productCode, long ptaskId, long locId, string remark)
    {
        if (ptaskId > 0)
        {
            _Task.BakAndDeleteTaskAndCmd(ptaskId, remark);
        }
        // 获取条码信息，没有添加
        var bean = _ProdProduct.CreateAbnormalProductEmptyBinding(productCode);
        if (bean is null)
        {
            return (0, null);
        }
        var skuId = bean.POProdSku.Id;
        var taskId = LocationCircularAbnormalTaskAndCmdRetTaskId(locId, skuId.GetValueOrDefault(), remark, productCode);
        //if (taskId > 0) 
        //{
        //    _WcsScanner.UpdateWcsScannerStatusByProductCode(productCode,(int)EnumScannerStatus.NoCreate,"生成异常任务")
        //}

        return (taskId, bean);
    }

    public long LocationAbnormalTaskAndCmdRetTaskId(long locId, long skuId, string remark, string productCode, int lineTaskNo = 0)
    {
        var taskId = _Task.CreateAbnormalResultTaskId(locId, skuId, remark, lineTaskNo);

        //if (taskId != 0)
        //{
        //    //同步MES
        //    SyncMes(productCode, locId, taskId);
        //}

        return taskId;
    }
    public long LocationCircularAbnormalTaskAndCmdRetTaskId(long locId, long skuId, string remark, string productCode, int lineTaskNo = 0)
    {
        var taskId = _Task.CreateCircularAbnormalResultTaskId(locId, skuId, remark, lineTaskNo);

        //if (taskId != 0)
        //{
        //    //同步MES
        //    SyncMes(productCode, locId, taskId);
        //}

        return taskId;
    }
    public long LocationOutAbnormalTaskAndCmdRetTaskId(long locId, long skuId, string remark, string productCode, int lineTaskNo = 0)
    {
        var taskId = _Task.CreateOutAbnormalResultTaskId(locId, skuId, remark, lineTaskNo);

        //if (taskId != 0)
        //{
        //    //同步MES
        //    SyncMes(productCode, locId, taskId);
        //}

        return taskId;
    }

    public long LocationAbnormalTaskAndCmdRetCmdId(long locId, long skuId, string remark,int taskNo, long equipId = 0)
    {
        var taskCmdId = _Task.CreateAbnormalResultCmdId(locId, skuId, remark, taskNo, equipId);
        return taskCmdId;
    }
}
